package cz.cernilovsky.android.profileapp.activities;

import java.io.IOException;

import org.json.JSONException;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

import cz.cernilovsky.android.profileapp.R;
import cz.cernilovsky.android.profileapp.R.string;
import cz.cernilovsky.android.profileapp.model.Action;
import cz.cernilovsky.android.profileapp.model.BluetoothAction;
import cz.cernilovsky.android.profileapp.model.Location;
import cz.cernilovsky.android.profileapp.model.Operation;
import cz.cernilovsky.android.profileapp.model.Profile;
import cz.cernilovsky.android.profileapp.model.SoundsAction;
import cz.cernilovsky.android.profileapp.model.WiFiAction;
import cz.cernilovsky.android.profileapp.services.LoadGeofencesService;
import cz.cernilovsky.android.profileapp.services.NotificationService;
import cz.cernilovsky.android.profileapp.services.MonitorGeofencesService;
import cz.cernilovsky.android.profileapp.storage.LocationsStorage;
import cz.cernilovsky.android.profileapp.storage.ProfilesStorage;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.DataSetObserver;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ToggleButton;

public class MainActivity extends FragmentActivity {
	private static final int CONNECTION_FAILURE = 9000;
	private static final String TAG = "MainActivity";
	public static final String MAIN_PREFERENCES = "main_preferences";
	public static final String STARTUP_PREFERENCE = "startup_preference";
	
	private boolean googleServicesAvailable() {
		int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
		if(resultCode == ConnectionResult.SUCCESS) {
			//Log.d("Geofence Detection", "Google Play services is available.");
			ToggleButton tb = ((ToggleButton) findViewById(R.id.toggleButton1));
			boolean enabled = getSharedPreferences(MAIN_PREFERENCES, MODE_PRIVATE).getBoolean(STARTUP_PREFERENCE, true);
			tb.setChecked(enabled);
			toggle(enabled);
			return true;
		}
		Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, CONNECTION_FAILURE);
		if(dialog != null) {
			dialog.setOnCancelListener(new Dialog.OnCancelListener() {
				
				@Override
				public void onCancel(DialogInterface dialog) {
					finish();
				}
			});
			dialog.show();
		}
		return false;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch(requestCode) {
		case CONNECTION_FAILURE:
			switch(resultCode) {
			case Activity.RESULT_OK:
				googleServicesAvailable();
				break;
			default:
				finish();
			}
			break;
		}
	}
	
	public void toggleButton(View view) {
		boolean checked = ((ToggleButton) view).isChecked();
		toggle(checked);
	}
	
	private void toggle(boolean checked) {
		SharedPreferences preferences = getSharedPreferences(MAIN_PREFERENCES, MODE_PRIVATE);
		preferences.edit().putBoolean(STARTUP_PREFERENCE, checked).commit();
		if(checked) {
			if(!MonitorGeofencesService.isStarted()) {
				Intent i = new Intent(this, MonitorGeofencesService.class);
				startService(i);
				//Log.d(TAG, "Starting LoadGeofencesService");
				i = new Intent(this, LoadGeofencesService.class);
				startService(i);
			}// else {
				//Log.d(TAG, "MonitorGeofencesService is already running");
			//}
		} else {
			Intent i = new Intent(this, MonitorGeofencesService.class);
			stopService(i);
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		/*Action[] actions = new Action[] {new BluetoothAction(Operation.OFF),
				new GPSAction(Operation.OFF),
				new SoundsAction(Operation.OFF),
				new WiFiAction(Operation.OFF)};
		for(Action a : actions) {
			a.perform(this);
		}*/
		
		setContentView(R.layout.activity_main);
		googleServicesAvailable();
		
		/*Profile on = new Profile(0, "SOUND OFF", new WiFiAction(Operation.NOOP), new SoundsAction(Operation.OFF), new BluetoothAction(Operation.NOOP), new GPSAction(Operation.NOOP));
	    Profile off = new Profile(1, "SOUND ON", new WiFiAction(Operation.NOOP), new SoundsAction(Operation.OFF), new BluetoothAction(Operation.NOOP), new GPSAction(Operation.NOOP));
		Profile[] onEntryProfiles = new Profile[] {on};
		Profile[] onLeaveProfiles = new Profile[] {off};
		Location l = new Location("TEST", onEntryProfiles, onLeaveProfiles, 50, 50, 50);
		try {
			ProfilesStorage.addProfile(on, this);
			ProfilesStorage.addProfile(off, this);
			LocationsStorage.addLocation(l, this);
		} catch (IOException e) {
			NotificationService.notifySettingsIOError(this);
			e.printStackTrace();
		} catch (JSONException e) {
			NotificationService.notifySettingsJSONError(this);
			e.printStackTrace();
		}*/
	}

}
